package hash;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

public class HashCodeTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashObj obj1 = new HashObj(123,1);
		HashObj obj2 = new HashObj(321,2);
		System.out.println(obj1.equals(obj2));
		System.out.println(obj1.hashCode());
		System.out.println(obj2.hashCode());
		Map<HashObj,String> map = new HashMap<HashObj,String>();
		map.put(obj1, "value : HashObj(1234,12)");
		map.put(obj2, "value : HashObj(1234,12)");
		System.out.println("----" + map.size());
		obj2.changeHash(122);

		Set<HashObj> keyset =map.keySet();
		for(HashObj hs : keyset){
			System.out.println(hs.getHash() + "----" + map.get(hs));
		}

	}

}


class HashObj{
	private int hash;
	public int tmp;
	HashObj(int hash,int tmp){
		this.hash = hash;
		this.tmp = tmp;
	}

	public void changeHash(int newHash){
		hash = newHash;
	}

	public int getHash(){
		return hash;
	}

	@Override
	public int hashCode(){
		return hash;
	}

	@Override
	public boolean equals(Object arg0) {
		if(this == arg0){
			return true;
		}
		if(!(arg0 instanceof HashObj)){
			return false;
		}
		return (arg0.hashCode() == this.hashCode());
	}



}
